package com.ruoyi.product.service.impl;

import com.ruoyi.product.domain.PmsCategory;
import com.ruoyi.product.dto.PmsCategoryDTO;
import com.ruoyi.product.mapper.PmsCategoryMapper;
import com.ruoyi.product.service.IPmsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 商品三级分类Service业务层处理
 * 
 * @author Dz
 * @date 2025-03-04
 */
@Service
public class PmsCategoryServiceImpl implements IPmsCategoryService 
{
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    /**
     * 查询商品三级分类
     * 
     * @param catId 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public PmsCategory selectPmsCategoryByCatId(Long catId)
    {
        return pmsCategoryMapper.selectPmsCategoryByCatId(catId);
    }

    /**
     * 查询商品三级分类列表
     * 
     * @param pmsCategory 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<PmsCategory> selectPmsCategoryList(PmsCategory pmsCategory)
    {
        return pmsCategoryMapper.selectPmsCategoryList(pmsCategory);
    }

    /**
     * 新增商品三级分类
     * 
     * @param pmsCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int insertPmsCategory(PmsCategory pmsCategory)
    {
        return pmsCategoryMapper.insertPmsCategory(pmsCategory);
    }

    /**
     * 修改商品三级分类
     * 
     * @param pmsCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int updatePmsCategory(PmsCategory pmsCategory)
    {
        return pmsCategoryMapper.updatePmsCategory(pmsCategory);
    }

    /**
     * 批量删除商品三级分类
     * 
     * @param catIds 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryByCatIds(Long[] catIds)
    {
        return pmsCategoryMapper.deletePmsCategoryByCatIds(catIds);
    }

    /**
     * 删除商品三级分类信息
     * 
     * @param catId 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryByCatId(Long catId)
    {
        return pmsCategoryMapper.deletePmsCategoryByCatId(catId);
    }

    @Override
    public List<PmsCategoryDTO> listTree() {

        List<PmsCategory> pmsCategories = pmsCategoryMapper.selectPmsCategoryList(null);

        List<PmsCategoryDTO> pmsCategoryDTOS = Optional.ofNullable(pmsCategories)
                .orElse(Collections.emptyList())  // 确保 pmsCategories 不是 null
                .stream()
                .map(PmsCategoryDTO::toDTO)
                .collect(Collectors.toList());

        pmsCategoryDTOS.stream().filter(category -> category.getParentCid() == 0)
                .map((m) -> {
                    m.setChildren(getChildren(m, pmsCategoryDTOS));
                    return m;
                })
                .collect(Collectors.toList());

        return pmsCategoryDTOS;
    }

    public List<PmsCategoryDTO> getChildren(PmsCategoryDTO category, List<PmsCategoryDTO> categories) {
        List<PmsCategoryDTO> children = categories.stream()
               .filter(c -> c.getParentCid().equals(category.getCatId()))
               .map(c -> {   // 递归获取子节点
                    c.setChildren(getChildren(c, categories));
                    return c;
                })
               .collect(Collectors.toList());
        return children;
    }

}
